import {
    setPublicKey,
    getPublicKey,
    getAccessToken,
    getRefreshToken,
    setRefreshToken,
    setAccessToken,
    removeRefreshToken,
    removeAccessToken
} from "@/utils/auth";
import {
    resetRouter,
} from "@/router";
import router from "@/router"
import {
    UserRegister,
    UserLogin,
    LogOutAccessToken,
    LogOutRefreshToken,
    GetCurrentUserInfo
} from "@/api/modules/user"
import qs from "qs"
import {
    getUserName,
    setUserName
} from "@/utils/Storage";
const getDefaultState = () => {
    return {
        accessToken: getAccessToken(),
        refreshToken: getRefreshToken(),
        username: getUserName(),
        publicKey: getPublicKey(),
        departmentId: "",
        userInfo: "",
    };
};

const state = getDefaultState();

const mutations = {
    LOGOUT: (state) => {
        // 同步token
        Object.assign(state, getDefaultState());
    },
    // 获取用户名
    SETUSERNAME: (state, username) => {
        state.username = username;
    },
    SETPUBLICKEY: (state, publickey) => {
        state.publickey = publickey;
    },
    GETDEPARTMENTID: (state, departmentId) => {
        state.departmentId = departmentId
    },
    SETACCESSTOKEN: (state, accessToken) => {
        state.accessToken = accessToken
    },
    SETREFRESHTOKEN: (state, refreshToken) => {
        state.refreshToken = refreshToken
    },
    // 获取用户信息
    GETUSERINFO: (state, userInfo) => {
        state.userInfo = userInfo
    }
};

const actions = {
    // 用户登录业务
    async Login({
        commit
    }, ruleList) {
        console.log(ruleList)
        const res = await UserLogin(ruleList)
        if (res.status == 200) {
            // 存储Token
            commit("SETACCESSTOKEN", res.data.AccessToken)
            commit("SETREFRESHTOKEN", res.data.RefreshToken)
            setAccessToken(res.data.AccessToken)
            setRefreshToken(res.data.RefreshToken)
            return "success"
        } else {
            return res.msg
        }
        // 存储token和用户名
        // commit("SET_TOKEN", token);
        // commit("SETUSERNAME", username);
        // setToken(token);
        // setUserName(username);

    },

    // 用户退出登录业务
    async LogOut({
        commit
    }) {
        return new Promise((resolve) => {
            LogOutAccessToken()
                .then(res1 => {
                    console.log(res1)
                    const refreshToken = getRefreshToken()
                    if (res1.status == 200) {
                        LogOutRefreshToken(refreshToken).then(res2 => {
                            console.log(res2)
                            if (res2.status == 200 || res2.status == 401) {
                                // 删除token
                                removeAccessToken();
                                removeRefreshToken();
                                resetRouter();
                                commit("RESET_STATE");
                                resolve();
                            } else {
                                reject(res2.msg)
                            }

                        }).catch((error) => {
                            reject(error);
                        })
                    } else {
                        console.log(res1.msg)
                    }

                })
                .catch((error) => {
                    reject(error);
                });
        });
    },
    // 用户登录业务
    async setPublicKey({
        commit
    }, publicKey) {
        // 存储token和用户名
        commit("SETPUBLICKEY", publicKey);
        setPublicKey(publicKey);
        return "ok";
    },
    // 用户注册业务
    async Register({
        commit
    }, ruleList) {
        console.log("ruleList", ruleList)
        const res = await UserRegister(ruleList)
        console.log("success", res)
        if (res.status == 200) {
            return "success"
        } else {
            return res.msg
        }
    },
    // 获取用户信息
    async GetUserInfo({
        commit
    }) {
        return new Promise((resolve, reject) => {
            GetCurrentUserInfo().then(res => {
                // console.log("userinfo", res)
                if (res.status == 200) {
                    commit("GETUSERINFO", res.data)
                }
                resolve()
            })

        })

    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
};